home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / indent.lha / indent / args.c next >
C/C++ Source or Header  |  1992-07-06  |  12KB  |  412 lines

  1. /*
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that the above copyright notice and this paragraph are
  9.  * duplicated in all such forms and that any documentation,
  10.  * advertising materials, and other materials related to such
  11.  * distribution and use acknowledge that the software was developed
  12.  * by the University of California, Berkeley, the University of Illinois,
  13.  * Urbana, and Sun Microsystems, Inc.  The name of either University
  14.  * or Sun Microsystems may not be used to endorse or promote products
  15.  * derived from this software without specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  17.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  */
  20.  
  21. #ifndef lint
  22. # ifndef OS2
  23.     static char sccsid[] = "@(#)args.c    6.0 (Berkeley) 92/06/15";
  24. # endif
  25. #endif                          /* not lint */
  26.  
  27. /*
  28.  * Argument scanning and profile reading code.  Default parameters are set
  29.  * here as well.
  30.  */
  31.  
  32. #include "globals.h"
  33. #include <sys/types.h>
  34. #include <ctype.h>
  35.  
  36. #ifdef OS2
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <malloc.h>
  40. #define INCL_NOPM
  41. #define INCL_NOPMAPI
  42. #define INCL_BASE
  43. #define INCL_DOSFILEMGR
  44. #include <os2.h>
  45. #define MAXPATHLEN CCHMAXPATH
  46. #define index(a,b) strchr(a,b)
  47. #else
  48. char *getenv(), *index();
  49.  
  50. #endif
  51.  
  52. #ifdef ANSIC
  53. static void scan_profile(FILE *);
  54. static FILE *fopenenv(char *, char *, char *);
  55.  
  56. #else
  57. static FILE *fopenenv();
  58.  
  59. #endif
  60.  
  61. /* profile types */
  62. #define    PRO_SPECIAL    1           /* special case */
  63. #define    PRO_BOOL    2             /* boolean */
  64. #define    PRO_INT        3             /* integer */
  65. #define PRO_FONT    4             /* troff font */
  66.  
  67. /* profile specials for booleans */
  68. #define    ON        1                 /* turn it on */
  69. #define    OFF        0                 /* turn it off */
  70.  
  71. /* profile specials for specials */
  72. #define    IGN        1                 /* ignore it */
  73. #define    CLI        2                 /* case label indent (float) */
  74. #define    STDIN        3               /* use stdin */
  75. #define    KEY        4                 /* type (keyword) */
  76. #define    CCI        5                 /* case code indent (float) */
  77.  
  78. /*
  79.  * N.B.: because of the way the table here is scanned, options whose names are
  80.  * substrings of other options must occur later; that is, with -lp vs -l, -lp
  81.  * must be first.  Also, while (most) booleans occur more than once, the last
  82.  * default value is the one actually assigned.
  83.  */
  84. struct pro
  85. {
  86.     char *p_name;               /* name, eg -bl, -cli */
  87.     int p_type;                 /* type (int, bool, special) */
  88.     int p_default;              /* the default value (if int) */
  89.     int p_special;              /* depends on type */
  90.     int *p_obj;                 /* the associated variable */
  91. }   pro[] =
  92.  
  93. {
  94.  
  95.     "T", PRO_SPECIAL, 0, KEY, 0,
  96.     "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation,
  97.     "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop,
  98.     "bad", PRO_BOOL, false, ON, &blanklines_after_declarations,
  99.     "bap", PRO_BOOL, false, ON, &blanklines_after_procs,
  100.     "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments,
  101.     "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
  102.     "bl", PRO_BOOL, false, OFF, &btype_2,
  103.     "brr", PRO_BOOL, false, ON, &btype_3,
  104.     "br", PRO_BOOL, true, ON, &btype_2,
  105.     "bs", PRO_BOOL, false, ON, &Bill_Shannon,
  106.     "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
  107.     "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
  108.     "ce", PRO_BOOL, true, ON, &cuddle_else,
  109.     "ci", PRO_INT, 0, 0, &continuation_indent,
  110.     "cli", PRO_SPECIAL, 0, CLI, 0,
  111.     "cci", PRO_SPECIAL, 0, CCI, 0,
  112.     "c", PRO_INT, 33, 0, &ps.com_ind,
  113.     "di", PRO_INT, 16, 0, &ps.decl_indent,
  114.     "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
  115.     "d", PRO_INT, 0, 0, &ps.unindent_displace,
  116.     "eei", PRO_BOOL, false, ON, &extra_expression_indent,
  117.     "ei", PRO_BOOL, true, ON, &ps.else_if,
  118.     "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
  119.     "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
  120.     "fb", PRO_FONT, 0, 0, (int *) &bodyf,
  121.     "fc1", PRO_BOOL, true, ON, &format_col1_comments,
  122.     "fc", PRO_FONT, 0, 0, (int *) &scomf,
  123.     "fk", PRO_FONT, 0, 0, (int *) &keywordf,
  124.     "fs", PRO_FONT, 0, 0, (int *) &stringf,
  125.     "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
  126.     "i", PRO_INT, 8, 0, &ps.ind_size,
  127.     "lc", PRO_INT, 0, 0, &block_comment_max_col,
  128.     "lp", PRO_BOOL, true, ON, &lineup_to_parens,
  129.     "l", PRO_INT, 78, 0, &max_col,
  130.     "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation,
  131.     "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop,
  132.     "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations,
  133.     "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs,
  134.     "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments,
  135.     "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
  136.     "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
  137.     "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline,
  138.     "nce", PRO_BOOL, true, OFF, &cuddle_else,
  139.     "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
  140.     "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
  141.     "nei", PRO_BOOL, true, OFF, &ps.else_if,
  142.     "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
  143.     "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
  144.     "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
  145.     "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
  146.     "npro", PRO_SPECIAL, 0, IGN, 0,
  147.     "nprs", PRO_BOOL, false, OFF, &parens_space,
  148.     "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
  149.     "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
  150.     "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
  151.     "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
  152.     "nv", PRO_BOOL, false, OFF, &verbose,
  153.     "pcs", PRO_BOOL, false, ON, &proc_calls_space,
  154.     "prs", PRO_BOOL, false, ON, &parens_space,
  155.     "psl", PRO_BOOL, true, ON, &procnames_start_line,
  156.     "ps", PRO_BOOL, false, ON, &pointer_as_binop,
  157.     "sc", PRO_BOOL, true, ON, &star_comment_cont,
  158.     "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines,
  159.     "st", PRO_SPECIAL, 0, STDIN, 0,
  160.     "tabs", PRO_INT, 8, 0, &tabsize,
  161.     "troff", PRO_BOOL, false, ON, &troff,
  162.     "v", PRO_BOOL, false, ON, &verbose,
  163.     "+", PRO_BOOL, false, ON, &cplus,
  164.     /* whew! */
  165.     0, 0, 0, 0, 0
  166. };
  167.  
  168. /*
  169.  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
  170.  * given in these files.
  171.  */
  172. #ifdef ANSIC
  173. void set_profile(void)
  174. #else
  175. set_profile()
  176. #endif
  177. {
  178.     FILE *f;
  179.  
  180. #ifdef OS2
  181. # ifdef __32BIT__
  182. #  define MODE_PROTECTED 1
  183. # endif
  184.     char MachineMode = MODE_PROTECTED;
  185.     static char prof[] = "indent.pro";
  186.  
  187. #else
  188.     static char prof[] = ".indent.pro";
  189.  
  190. #endif
  191.  
  192.     f = fopenenv(prof, "r", "HOME");
  193.  
  194. #ifdef OS2
  195. # ifndef __32BIT__                  /* Must be MSDOS or OS/2 1.x code */
  196.     DosGetMachineMode(&MachineMode);
  197. # endif
  198.     if (MachineMode == MODE_PROTECTED)
  199.         if (f == NULL)
  200.             f = fopenenv(prof, "r", "DPATH");
  201. #endif
  202.     if (f == NULL)
  203.         f = fopenenv(prof, "r", "PATH");
  204.  
  205.     if (f != NULL)
  206.     {
  207.         scan_profile(f);
  208.         (void) fclose(f);
  209.     }
  210.  
  211.     if ((f = fopen(prof, "r")) != NULL)
  212.     {
  213.         scan_profile(f);
  214.         (void) fclose(f);
  215.     }
  216. }
  217.  
  218. #ifdef ANSIC
  219. static void scan_profile(FILE *f)
  220. #else
  221. scan_profile(f)
  222.     FILE *f;
  223.  
  224. #endif
  225. {
  226.     register int i;
  227.     register char *p;
  228.     char buf[BUFSIZ];
  229.  
  230.     while (1)
  231.     {
  232.         for (p = buf; (i = getc(f)) != EOF && (*p = (char) i) > ' '; ++p);
  233.         if (p != buf)
  234.         {
  235.             *p++ = 0;
  236.             if (verbose)
  237.                 printf("profile: %s\n", buf);
  238.             set_option(buf);
  239.         }
  240.         else if (i == EOF)
  241.             return;
  242.     }
  243. }
  244.  
  245. static char *param_start;
  246.  
  247. #ifdef ANSIC
  248. static int eqin(register char *s1, register char *s2)
  249. #else
  250. eqin(s1, s2)
  251.     register char *s1;
  252.     register char *s2;
  253.  
  254. #endif
  255. {
  256.     while (*s1)
  257.     {
  258.         if (*s1++ != *s2++)
  259.             return (false);
  260.     }
  261.     param_start = s2;
  262.     return (true);
  263. }
  264.  
  265. /*
  266.  * Set the defaults.
  267.  */
  268. #ifdef ANSIC
  269. void set_defaults(void)
  270. #else
  271. set_defaults()
  272. #endif
  273. {
  274.     register struct pro *p;
  275.  
  276.     /*
  277.        Because ps.case_indent and ps.case_code_indent are floats, we can't
  278.        initialize them from the table:
  279.     */
  280.     ps.case_indent = (float) 0; /* -cli0.0 */
  281.     ps.case_code_indent = (float) 1;  /* -cci1.0 */
  282.     for (p = pro; p->p_name != NULL; p++)
  283.         if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
  284.             *p->p_obj = p->p_default;
  285. }
  286.  
  287. #ifdef ANSIC
  288. void set_option(register char *arg)
  289. #else
  290. set_option(arg)
  291.     register char *arg;
  292.  
  293. #endif
  294. {
  295.     register struct pro *p;
  296.  
  297. #ifndef ANSIC
  298.     extern double atof();
  299.  
  300. #endif
  301.     arg++;                      /* ignore leading "-" */
  302.     for (p = pro; p->p_name; p++)
  303.         if (*p->p_name == *arg && eqin(p->p_name, arg))
  304.             goto found;
  305.     fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
  306.     exit(1);
  307. found:
  308.     switch (p->p_type)
  309.     {
  310.  
  311.     case PRO_SPECIAL:
  312.         switch (p->p_special)
  313.         {
  314.  
  315.         case IGN:
  316.             break;
  317.  
  318.         case CLI:
  319.             if (*param_start == 0)
  320.                 goto need_param;
  321.             ps.case_indent = (float) atof(param_start);
  322.             break;
  323.  
  324.         case CCI:
  325.             if (*param_start == 0)
  326.                 goto need_param;
  327.             ps.case_code_indent = (float) atof(param_start);
  328.             break;
  329.  
  330.         case STDIN:
  331.             if (input == NULL)
  332.                 input = stdin;
  333.             if (output == NULL)
  334.                 output = stdout;
  335.             break;
  336.  
  337.         case KEY:
  338.             if (*param_start == 0)
  339.                 goto need_param;
  340.             {
  341.                 register char *str = (char *) malloc(strlen(param_start) + 1);
  342.  
  343.                 strcpy(str, param_start);
  344.                 addkey(str, 4);
  345.             }
  346.             break;
  347.  
  348.         default:
  349.             fprintf(stderr,
  350.                     "indent: set_option: internal error: p_special %d\n",
  351.                     p->p_special);
  352.             exit(1);
  353.         }
  354.         break;
  355.  
  356.     case PRO_BOOL:
  357.         if (p->p_special == OFF)
  358.             *p->p_obj = false;
  359.         else
  360.             *p->p_obj = true;
  361.         break;
  362.  
  363.     case PRO_INT:
  364.         if (*param_start == 0)
  365.         {
  366.     need_param:
  367.             fprintf(stderr, "indent: \"%s\" requires a parameter\n",
  368.                     arg - 1);
  369.             exit(1);
  370.         }
  371.         *p->p_obj = atoi(param_start);
  372.         break;
  373.  
  374.     case PRO_FONT:
  375.         parsefont((struct fstate *) p->p_obj, param_start);
  376.         break;
  377.  
  378.     default:
  379.         fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
  380.                 p->p_type);
  381.         exit(1);
  382.     }
  383. }
  384.  
  385. /*------------------------------------------------------------------------------
  386.  
  387.   FILE *fopenenv(char *name, char *mode, char *env)
  388.  
  389.   Similar to fopen() but searches the directories listed in environment
  390.   item 'env' for the file.
  391.  
  392.   Written 14 April 1992 by Jon Saxton who is fairly sure that a function
  393.   like this already exists somewhere but who couldn't find one.
  394.  
  395. ------------------------------------------------------------------------------*/
  396.  
  397. #ifdef ANSIC
  398. FILE *fopenenv(char *name, char *mode, char *env)
  399. #else
  400. FILE *fopenenv(name, mode, env)
  401.     char *name, *mode, *env;
  402.  
  403. #endif
  404. {
  405.     char FullName[MAXPATHLEN];
  406.  
  407.     _searchenv(name, env, FullName);
  408.     if (strlen(FullName) == 0)
  409.         return NULL;
  410.     return fopen(FullName, mode);
  411. }
  412.